//
//  ShortcutDetailViewController.m
//  DemoApplicationShortcut
//
//  Created by 唐可雄 on 16/12/5.
//  Copyright © 2016年 swustyc. All rights reserved.
//

#import "ShortcutDetailViewController.h"

@interface ShortcutDetailViewController ()<UITextFieldDelegate, UIPickerViewDataSource> {
    NSArray *pickerItems;
}

@property (weak, nonatomic) IBOutlet UITextField * titleTextField;
@property (weak, nonatomic) IBOutlet UITextField * subtitleTextField;
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *doneButton;

@end

@implementation ShortcutDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    pickerItems = [NSArray arrayWithObjects:@"Compose", @"Play", @"Pause", @"Add", @"Location", @"Search", @"Share", nil];
    
    self.title = self.shortcutItem.localizedTitle;
    
    self.titleTextField.text = self.shortcutItem.localizedTitle;
    self.subtitleTextField.text = self.shortcutItem.localizedSubtitle;
    
    id iconRawValue = self.shortcutItem.userInfo[@"applicationShortcutUserInfoIconKey"];
    [self.pickerView selectRow:[iconRawValue integerValue] inComponent:0 animated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textfieldTextDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - Notification

- (void)textfieldTextDidChange:(UITextField *)textfield {
    self.doneButton.enabled = textfield.text.length > 0;
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    return YES;
}

#pragma mark - UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return pickerItems.count;
}

#pragma mark - UIStoryboardSegue Handling

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"ShortcutDetailUpdated"]) {
        // 在修改的情况下，创建一个来表示最终状态
        self.shortcutItem = [[UIApplicationShortcutItem alloc] initWithType:self.shortcutItem.type localizedTitle:self.titleTextField.text localizedSubtitle:self.subtitleTextField.text icon:[UIApplicationShortcutIcon iconWithType:0] userInfo:@{@"applicationShortcutUserInfoIconKey":@([self.pickerView selectedRowInComponent:0])}];
    }
}

@end
